package framework.util;

import java.io.InputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.Properties;
import java.util.Enumeration;
import org.apache.log4j.Category;


public class Value {

  private static Category log = Category.getInstance(Value.class.getName());
  private static Properties properties = null;

  //properties file loading
  private static void loadProperties() {
    log.info("loading properties file : framework.properties");
    InputStream inputStream = Value.class.getResourceAsStream("framework.properties");
    if (inputStream==null) {
      log.error("chargement des proprietes impossible : framework.properties manquant");
    } else {
      try {
        properties = new Properties();
        properties.load(inputStream);
      } catch (IOException ie) {
        ie.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  //return a property value or null if key does not exit
  public static String getProperty(String key) {
    if (properties==null)
      loadProperties();
    String str = null;
    if (properties==null) {
      log.error("proprietes non chargee");
    } else {
      str = properties.getProperty(key);
    }
    log.debug("getProperty : "+key+" - "+str);
    return str;
  }

  //return a property value Vector or null if partial key does not exit
  public static Vector getPropertiesList(String partKey) {
    if (properties==null)
      loadProperties();
    Vector list = null;
    if (properties==null) {
      log.error("proprietes non chargee");
    } else {
      list = new Vector();
      Enumeration enumeration = properties.propertyNames();
      int length = partKey.length();
      String key;
      while (enumeration.hasMoreElements()) {
        key = (String)enumeration.nextElement();
        if ( key.length()>=length && key.substring(0,length).equals(partKey) )
          list.addElement( properties.getProperty(key) );
      }
    }
    return list;
  }

}